package com.test.javafaq_sys.controller;


import com.test.javafaq_commons.entity.User;
import com.test.javafaq_commons.service.ServiceException;
import com.test.javafaq_commons.vo.R;
import com.test.javafaq_sys.service.IUserService;
import com.test.javafaq_sys.vo.RegisterVo;
import com.test.javafaq_sys.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test.com
 * @since 2025-04-11
 */
@RestController
@RequestMapping("/v1/users")
@Slf4j
public class UserController {

    @Autowired
    IUserService userService;

    @GetMapping("/master")
    public List<User> master(){
        return userService.getTeachers();
    }

//    @GetMapping("/me")
//    public R<UserVo> me(){
//        //测试页面： {"code":200,"message":"OK","data":{"id":11,"username":"st2","nickname":"李四同学","questions":21,"collections":0}}
//        UserVo userVo = userService.currentUserVo();
//        return R.ok(userVo);
//    }

    @GetMapping("/me")
    public R<UserVo> me(@AuthenticationPrincipal UserDetails user){
        System.out.println("-------------------me---------user = " + user);
        UserVo userVo = userService.currentUserVo(user.getUsername());
        return R.ok(userVo);
    }

    @GetMapping("/teacher")
    public R<List<User>> teacher(){
        List<User> teachers = userService.getTeachers();
        return R.ok(teachers);
    }


    //localhost:8080/v1/users/get?id=1
    @GetMapping("/get")
    @PreAuthorize("hasAuthority('/user/get')") //设置访问这个权限的方法
    public User get(Integer id){
        User u = userService.getById(id);
        return u;
    }
    //localhost:8080/v1/users/list
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/user/list')")
    public List<User> list(){
        List<User> list = userService.list();
        return list;
    }

    @PostMapping("/register")
    public R registerStudent(@Validated RegisterVo registerVo, BindingResult res){
        //@Validated RegisterVo registerVo 表示要验证RegisterVo
        //BindingResult 来保存验证结果

        if(res.hasErrors()){//如果有错误
            String error = res.getFieldError().getDefaultMessage();
            return R.unproecsableEntity(error);
        }
        if(!registerVo.getPassword().equals(registerVo.getConfirm())){
            return R.unproecsableEntity("两次密码输入不一致");
        }

        log.debug("得到表单:{}",registerVo);
        try {
            userService.registerStudent(registerVo);
            return R.created("注册成功");
        }catch (ServiceException e){
            return R.failed(e);
        }
    }

}
